/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * 
 */
package dgbgui.actions;

import dgbgui.command.CommandStack;
import dgbgui.gui.MainFrame;
import dgbgui.modeldocument.ModelDocument;
import dgbgui.modeldocument.definition.ClosureDefinition;
import dgbgui.modeldocument.definition.CustomDefinition;
import dgbgui.modeldocument.definition.FilteredInstanceDefinition;
import dgbgui.modeldocument.definition.NeighbourhoodDefinition;
import dgbgui.selection.SelectionManager;
import dgbgui.tools.ChangePerspectiveTool;
import dgbgui.tools.ClearModelTool;
import dgbgui.tools.ClosureTool;
import dgbgui.tools.CopyTool;
import dgbgui.tools.CreateDefaultTypesTool;
import dgbgui.tools.CreateFromResultTool;
import dgbgui.tools.CreateIntersectionTool;
import dgbgui.tools.CreateNotTool;
import dgbgui.tools.CreateSetTool;
import dgbgui.tools.CreateUnionTool;
import dgbgui.tools.CreateXORTool;
import dgbgui.tools.CustomQueryTool;
import dgbgui.tools.CutTool;
import dgbgui.tools.DGBSettingsTool;
import dgbgui.tools.DeleteTool;
import dgbgui.tools.EditConceptronInstancePrototypeTool;
import dgbgui.tools.ExecuteQueryTool;
import dgbgui.tools.LoadTool;
import dgbgui.tools.NeighbourhoodTool;
import dgbgui.tools.PasteTool;
import dgbgui.tools.RenameTool;
import dgbgui.tools.SaveTool;
import dgbgui.tools.SetTargetTool;
import dgbgui.tools.Tool;
import dgbgui.tools.UpdateIORViaHttpTool;

/**
 * This class is a singleton for action events. It listens for events
 * and launches to correct function as a result
 * @author bjnortier
 *
 */
public class ActionProxy {

	// Private for singleton
    private ActionProxy() {
    }
    
	static public ActionProxy getInstance() {
		if ( m_ActionProxy == null ) {
			m_ActionProxy = new ActionProxy();
		}
		return m_ActionProxy;
	}
	
	public void actionInitiated(ActionRepository.ActionName actionName) {
		
		Tool tool = null;
		switch (actionName) {
        case FILE_NEW:
            tool = new ClearModelTool();
            break;
        case FILE_OPEN:
            tool = new LoadTool();
            break;
        case FILE_SAVE:
            tool = new SaveTool(ModelDocument.getInstance().getFileName());
            break;
        case FILE_SAVE_AS:
            tool = new SaveTool();
            break;
        case EDIT_UNDO:
            CommandStack.getInstance().undo();
            break;
        case EDIT_REDO:
            CommandStack.getInstance().redo();
            break;
        case EDIT_COPY:
            tool = new CopyTool();
            break;
        case EDIT_CUT:
            tool = new CutTool();
            break;
        case EDIT_PASTE:
            tool = new PasteTool();
            break;
		case EDIT_DELETE : 
			tool = new DeleteTool();
			break;
		case EDIT_RENAME :
            tool = new RenameTool();
            break;
		case EDIT_MODIFY :
			tool = createEditToolFromSelection();
			break;
		case QUERY_CREATE_SET : 
			tool = new CreateSetTool();
			break;
        case QUERY_CREATE_FROM_RESULT : 
            tool = new CreateFromResultTool();
            break;
        case QUERY_EXECUTE :
            tool = new ExecuteQueryTool();
            break;
        case QUERY_CREATE_CUSTOM :
            tool = new CustomQueryTool(CustomQueryTool.ToolType.CREATE);
            break;
        case QUERY_CREATE_NEIGHBOURHOOD :
            tool = new NeighbourhoodTool(NeighbourhoodTool.ToolType.CREATE);
            break;
        case QUERY_CREATE_CLOSURE :
            tool = new ClosureTool(ClosureTool.ToolType.CREATE);
            break;
		case BOOLEAN_INTERSECT : 
			tool = new CreateIntersectionTool();
			break;
		case BOOLEAN_UNION : 
			tool = new CreateUnionTool();
			break;
		case BOOLEAN_NOT : 
			tool = new CreateNotTool();
			break;
        case BOOLEAN_XOR : 
            tool = new CreateXORTool();
            break;
        case DGB_SETTINGS :
            tool = new DGBSettingsTool();
            break;
        case DGB_GET_IOR_VIA_HTTP :
            tool = new UpdateIORViaHttpTool();
            break;
        case QUERY_ADD_TYPES :
            tool = new CreateDefaultTypesTool();
            break;
        case PERSPECTIVE_DEFAULT :
            tool = new ChangePerspectiveTool(MainFrame.Perspective.DEFAULT);
            break;
        case PERSPECTIVE_MODEL :
            tool = new ChangePerspectiveTool(MainFrame.Perspective.MODELDRIVEN);
            break;
        case MODEL_SET_TARGET :
            tool = new SetTargetTool();
            break;
		default: // do nothing
		}
		
		if ( tool != null ) {
			tool.execute();
		}
		
	}
    
    private Tool createEditToolFromSelection() {
        assert ( SelectionManager.getInstance().size() == 1 );
        if ( SelectionManager.getInstance().getDefinitionFilteredSelection(FilteredInstanceDefinition.class).size() == 1 ) {
            return new EditConceptronInstancePrototypeTool();
        } 
        if ( SelectionManager.getInstance().getDefinitionFilteredSelection(CustomDefinition.class).size() == 1 ) {
            return new CustomQueryTool(CustomQueryTool.ToolType.EDIT);
        } 
        if ( SelectionManager.getInstance().getDefinitionFilteredSelection(NeighbourhoodDefinition.class).size() == 1 ) {
            return new NeighbourhoodTool(NeighbourhoodTool.ToolType.EDIT);
        } 
        if ( SelectionManager.getInstance().getDefinitionFilteredSelection(ClosureDefinition.class).size() == 1 ) {
            return new ClosureTool(ClosureTool.ToolType.EDIT);
        } 
        // No tool found for selection
        assert false;
        return null;
    }
    
	
	static private ActionProxy m_ActionProxy; 
}
